package com.axin.blog.controller;

import com.axin.blog.api.domain.BlogCategory;
import com.axin.blog.api.domain.BlogTag;
import com.axin.blog.service.IBlogTagService;
import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.security.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客标签 接口控制
 *
 * @Author axin
 * @Date 2021/10/29 17:10
 **/
@RestController
@RequestMapping("/tag")
public class BlogTagController extends BaseController {

    @Autowired
    private IBlogTagService tagService;


    /**
     * 根据参数分页查询博客标签列表
     */
    @PreAuthorize("@ss.hasPermi('blog:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogTag tag) {
        return tagService.selectPageBlogTagList(tag);
    }

    /**
     * 获取前端下拉可选标签列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:tag:list')")
    @GetMapping("/getTagList")
    public AjaxResult<List<BlogTag>> list() {
        return AjaxResult.success(tagService.selectBlogTagList(new BlogTag()));
    }

    /**
     * 新增博客标签
     */
    @PreAuthorize("@ss.hasPermi('blog:tag:add')")
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody BlogTag tag) {
        if (!tagService.checkTagNameUnique(tag)) {
            return AjaxResult.error("新增标签'" + tag.getTagName() + "'失败，标签名已存在");
        }
        return toAjax(tagService.insertBlogTag(tag));
    }


    /**
     * 根据标签ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:tag:query')")
    @GetMapping(value = "/{tagId}")
    public AjaxResult getInfo(@PathVariable Long tagId) {
        return AjaxResult.success(tagService.selectBlogTagById(tagId));
    }

    /**
     * 修改标签
     */
    @PreAuthorize("@ss.hasPermi('blog:tag:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BlogTag tag) {
        if (!tagService.checkTagNameUnique(tag)) {
            return AjaxResult.error("修改标签'" + tag.getTagName() + "'失败，标签名已存在");
        }
        return toAjax(tagService.updateBlogTag(tag));
    }


    /**
     * 删除标签
     */
    @PreAuthorize("@ss.hasPermi('blog:tag:remove')")
    @DeleteMapping("/{tagIds}")
    public AjaxResult remove(@PathVariable Long[] tagIds) {
        tagService.deleteTagByIds(tagIds);
        return success();
    }

    /************************前台接口******************************************************************************************/

    /**
     * 获取标签归档
     *
     * @return
     */
    @GetMapping("/getWebTagList")
    public AjaxResult<List<BlogTag>> getWebTagList() {
        return AjaxResult.success(tagService.getTagArchiveList());
    }


}
